package com.bocai.yuweiwangluo.fragment;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.yuweiwangluo.R;
import com.bocai.yuweiwangluo.activity.VenueBookActivity;
import com.bocai.yuweiwangluo.base.BaseFragment;
import com.bocai.yuweiwangluo.bean.HomeBean;
import com.bocai.yuweiwangluo.utils.ImageLoaderUtil;
import com.bocai.yuweiwangluo.utils.UrlData;

import java.text.DecimalFormat;
import java.util.ArrayList;

import butterknife.ButterKnife;


/**
 * Created by admin on 2015/9/16.
 */
public class FragmentRecommendVenue extends BaseFragment implements View.OnClickListener {
    private View view;
    private LinearLayout ll_home_recommend_venue1;
    private LinearLayout ll_home_recommend_venue2;
    private ImageView ivVenue1Img;
    private TextView tvVenue1Name;
    private TextView tvVenue1Distance;
    private TextView tvVenue1Content;
    private TextView tvVenue1Money;
    private TextView tvVenue1Sold;
    private ImageView ivVenue2Img;
    private TextView tvVenue2Name;
    private TextView tvVenue2Distance;
    private TextView tvVenue2Content;
    private TextView tvVenue2Money;
    private TextView tvVenue2Sold;
    private TextView tvPrice1,tvPrice2;

    private ArrayList<HomeBean.Recommand> datas;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        view = inflater.inflate(R.layout.fragment_recommend_venue, container, false);
        initView();
        return view;
    }
    public void getData(ArrayList<HomeBean.Recommand> data)
    {
        this.datas=data;


    }

    private void initData()
    {
        try
        {
            DecimalFormat df = new DecimalFormat("0.0");
            tvVenue1Name.setText(datas.get(0).getTitle());
            tvVenue1Distance.setText(df.format(Double.parseDouble(datas.get(0).getMin_local()))+"km");//距离
            tvVenue1Content.setText(datas.get(0).getContent());
            tvVenue1Money.setText(Math.round(Double.parseDouble(datas.get(0).getItem_price()))+"");
            tvPrice1.setText("原价"+Math.round(Double.parseDouble(datas.get(0).getPrice()))+"元");
            if(datas.get(0).getSale_num()==null)
            {
                tvVenue1Sold.setText("已售 0");
            }else
            {

                tvVenue1Sold.setText("已售 " + datas.get(0).getSale_num());
            }
            ImageLoaderUtil.loaderImageDefault(UrlData.IMAGEURL + datas.get(0).getPic(), ivVenue1Img);
            if(datas.size()>1)
            {
                ll_home_recommend_venue2.setVisibility(View.VISIBLE);
                ImageLoaderUtil.loaderImageDefault(UrlData.IMAGEURL + datas.get(1).getPic(), ivVenue2Img);
                tvVenue2Name.setText(datas.get(1).getTitle());
                tvVenue2Distance.setText(df.format(Double.parseDouble(datas.get(1).getMin_local()))+"km");//距离
                tvVenue2Content.setText(datas.get(1).getContent());
                tvVenue2Money.setText(Math.round(Double.parseDouble(datas.get(1).getItem_price()))+"");
                tvPrice2.setText("原价"+Math.round(Double.parseDouble(datas.get(1).getPrice()))+"元");
                if(datas.get(1).getSale_num()==null)
                {
                    tvVenue1Sold.setText("已售 0");
                }else
                {

                    tvVenue2Sold.setText("已售"+datas.get(1).getSale_num());
                }
            } else
            {
                ll_home_recommend_venue2.setVisibility(View.GONE);
            }
        }catch (Exception e)
        {
        }


    }

    private void initView()
    {
        ButterKnife.bind(this, view);
        ll_home_recommend_venue1 = (LinearLayout)view.findViewById(R.id.ll_home_recommend_venue1);
        ll_home_recommend_venue2=(LinearLayout)view.findViewById(R.id.ll_home_recommend_venue2);
        ivVenue1Img=(ImageView)view.findViewById(R.id.iv_home_recommend_venue1_img);
        tvVenue1Name=(TextView)view.findViewById(R.id.tv_home_recommend_venue1_name);
        tvVenue1Distance=(TextView)view.findViewById(R.id.tv_home_recommend_venue1_distance);
        tvVenue1Content=(TextView)view.findViewById(R.id.tv_home_recommend_venue1_content);
        tvVenue1Money=(TextView)view.findViewById(R.id.tv_home_recommend_venue1_money);
        tvVenue1Sold=(TextView)view.findViewById(R.id.tv_home_recommend_venue1_sold);
        ivVenue2Img=(ImageView)view.findViewById(R.id.iv_home_recommend_venue2_img);
        tvVenue2Name=(TextView)view.findViewById(R.id.tv_home_recommend_venue2_name);
        tvVenue2Distance=(TextView)view.findViewById(R.id.tv_home_recommend_venue2_distance);
        tvVenue2Content=(TextView)view.findViewById(R.id.tv_home_recommend_venue2_content);
        tvVenue2Money=(TextView)view.findViewById(R.id.tv_home_recommend_venue2_money);
        tvVenue2Sold =(TextView)view.findViewById(R.id.tv_home_recommend_venue2_sold);
        tvPrice1=(TextView)view.findViewById(R.id.tv_price1);
        tvPrice2=(TextView)view.findViewById(R.id.tv_price2);
        tvPrice1.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        tvPrice2.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        initEvent();
        initData();
    }

    private void initEvent()
    {
        ll_home_recommend_venue1.setOnClickListener(this);
        ll_home_recommend_venue2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id. ll_home_recommend_venue1:
                start(0);
                break;
            case R.id.ll_home_recommend_venue2:
                start(1);

                break;
        }
    }

    private void start(int position)
    {
        Intent intent=new Intent(getActivity(), VenueBookActivity.class);
        intent.putExtra("id", datas.get(position).getId());
        intent.putExtra("title",datas.get(position).getTitle());
        intent.putExtra("content",datas.get(position).getContent());
        intent.putExtra("local_x",datas.get(position).getLocal_x());
        intent.putExtra("local_y",datas.get(position).getLocal_y());
        startActivity(intent);
    }

    @Override
    public void onDestroyView()
    {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
